home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / zsh / Source / src / zle_move.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-07  |  5.6 KB  |  391 lines

  1. /*
  2.  *
  3.  * zle_move.c - editor movement
  4.  *
  5.  * This file is part of zsh, the Z shell.
  6.  *
  7.  * This software is Copyright 1992 by Paul Falstad
  8.  *
  9.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  10.  * use this software as long as: there is no monetary profit gained
  11.  * specifically from the use or reproduction of this software, it is not
  12.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  13.  * included prominently in any copy made.
  14.  *
  15.  * The author make no claims as to the fitness or correctness of this software
  16.  * for any use whatsoever, and it is provided as is. Any use of this software
  17.  * is at the user's own risk.
  18.  *
  19.  */
  20.  
  21. #define ZLE
  22. #include "zsh.h"
  23.  
  24. static vimarkcs[27], vimarkline[27];
  25.  
  26. void beginningofline()
  27. {                /**/
  28.     if (mult < 0) {
  29.     mult = -mult;
  30.     endofline();
  31.     return;
  32.     }
  33.     while (mult--) {
  34.     if (cs == 0)
  35.         return;
  36.     if (line[cs - 1] == '\n')
  37.         if (!--cs)
  38.         return;
  39.     while (cs && line[cs - 1] != '\n')
  40.         cs--;
  41.     }
  42. }
  43.  
  44. void endofline()
  45. {                /**/
  46.     if (mult < 0) {
  47.     mult = -mult;
  48.     beginningofline();
  49.     return;
  50.     }
  51.     while (mult--) {
  52.     if (cs >= ll) {
  53.         cs = ll;
  54.         return;
  55.     }
  56.     if (line[cs] == '\n')
  57.         if (++cs == ll)
  58.         return;
  59.     while (cs != ll && line[cs] != '\n')
  60.         cs++;
  61.     }
  62. }
  63.  
  64. void beginningoflinehist()
  65. {                /**/
  66.     if (mult < 0) {
  67.     mult = -mult;
  68.     endoflinehist();
  69.     return;
  70.     }
  71.     while (mult) {
  72.     if (cs == 0)
  73.         break;
  74.     if (line[cs - 1] == '\n')
  75.         if (!--cs)
  76.         break;
  77.     while (cs && line[cs - 1] != '\n')
  78.         cs--;
  79.     mult--;
  80.     }
  81.     if (mult) {
  82.     uphistory();
  83.     cs = 0;
  84.     }
  85. }
  86.  
  87. void endoflinehist()
  88. {                /**/
  89.     if (mult < 0) {
  90.     mult = -mult;
  91.     beginningoflinehist();
  92.     return;
  93.     }
  94.     while (mult) {
  95.     if (cs >= ll) {
  96.         cs = ll;
  97.         break;
  98.     }
  99.     if (line[cs] == '\n')
  100.         if (++cs == ll)
  101.         break;
  102.     while (cs != ll && line[cs] != '\n')
  103.         cs++;
  104.     mult--;
  105.     }
  106.     if (mult)
  107.     downhistory();
  108. }
  109.  
  110. void forwardchar()
  111. {                /**/
  112.     cs += mult;
  113.     if (cs > ll)
  114.     cs = ll;
  115.     if (cs < 0)
  116.     cs = 0;
  117. }
  118.  
  119. void backwardchar()
  120. {                /**/
  121.     cs -= mult;
  122.     if (cs > ll)
  123.     cs = ll;
  124.     if (cs < 0)
  125.     cs = 0;
  126. }
  127.  
  128. void setmarkcommand()
  129. {                /**/
  130.     mark = cs;
  131. }
  132.  
  133. void exchangepointandmark()
  134. {                /**/
  135.     int x;
  136.  
  137.     x = mark;
  138.     mark = cs;
  139.     cs = x;
  140.     if (cs > ll)
  141.     cs = ll;
  142. }
  143.  
  144. void vigotocolumn()
  145. {                /**/
  146.     int x, y, ocs = cs;
  147.  
  148.     if (mult > 0)
  149.     mult--;
  150.     findline(&x, &y);
  151.     if (mult >= 0)
  152.     cs = x + mult;
  153.     else
  154.     cs = y + mult;
  155.     if (cs < x || cs > y) {
  156.     feep();
  157.     cs = ocs;
  158.     }
  159. }
  160.  
  161. void vimatchbracket()
  162. {                /**/
  163.     int ocs = cs, dir, ct;
  164.     unsigned char oth, me;
  165.  
  166.   otog:
  167.     if (cs == ll) {
  168.     feep();
  169.     cs = ocs;
  170.     return;
  171.     }
  172.     switch (me = line[cs]) {
  173.     case '{':
  174.     dir = 1;
  175.     oth = '}';
  176.     break;
  177.     case '}':
  178.     dir = -1;
  179.     oth = '{';
  180.     break;
  181.     case '(':
  182.     dir = 1;
  183.     oth = ')';
  184.     break;
  185.     case ')':
  186.     dir = -1;
  187.     oth = '(';
  188.     break;
  189.     case '[':
  190.     dir = 1;
  191.     oth = ']';
  192.     break;
  193.     case ']':
  194.     dir = -1;
  195.     oth = '[';
  196.     break;
  197.     default:
  198.     cs++;
  199.     goto otog;
  200.     }
  201.     ct = 1;
  202.     while (cs >= 0 && cs < ll && ct) {
  203.     cs += dir;
  204.     if (line[cs] == oth)
  205.         ct--;
  206.     else if (line[cs] == me)
  207.         ct++;
  208.     }
  209.     if (cs < 0 || cs >= ll) {
  210.     feep();
  211.     cs = ocs;
  212.     }
  213. }
  214.  
  215. void viforwardchar()
  216. {                /**/
  217.     if (mult < 0) {
  218.     mult = -mult;
  219.     vibackwardchar();
  220.     return;
  221.     }
  222.     while (mult--) {
  223.     cs++;
  224.     if (cs >= ll || line[cs] == '\n') {
  225.         cs--;
  226.         break;
  227.     }
  228.     }
  229. }
  230.  
  231. void vibackwardchar()
  232. {                /**/
  233.     if (mult < 0) {
  234.     mult = -mult;
  235.     viforwardchar();
  236.     return;
  237.     }
  238.     while (mult--) {
  239.     cs--;
  240.     if (cs < 0 || line[cs] == '\n') {
  241.         cs++;
  242.         break;
  243.     }
  244.     }
  245. }
  246.  
  247. void viendofline()
  248. {                /**/
  249.     cs = findeol();
  250.     if (!virangeflag && cs != 0 && line[cs - 1] != '\n')
  251.     cs--;
  252. }
  253.  
  254. void vibeginningofline()
  255. {                /**/
  256.     cs = findbol();
  257. }
  258.  
  259. static int vfindchar, vfinddir, tailadd;
  260.  
  261. void vifindnextchar()
  262. {                /**/
  263.     if ((vfindchar = vigetkey())) {
  264.     vfinddir = 1;
  265.     tailadd = 0;
  266.     virepeatfind();
  267.     }
  268. }
  269.  
  270. void vifindprevchar()
  271. {                /**/
  272.     if ((vfindchar = vigetkey())) {
  273.     vfinddir = -1;
  274.     tailadd = 0;
  275.     virepeatfind();
  276.     }
  277. }
  278.  
  279. void vifindnextcharskip()
  280. {                /**/
  281.     if ((vfindchar = vigetkey())) {
  282.     vfinddir = 1;
  283.     tailadd = -1;
  284.     virepeatfind();
  285.     }
  286. }
  287.  
  288. void vifindprevcharskip()
  289. {                /**/
  290.     if ((vfindchar = vigetkey())) {
  291.     vfinddir = -1;
  292.     tailadd = 1;
  293.     virepeatfind();
  294.     }
  295. }
  296.  
  297. void virepeatfind()
  298. {                /**/
  299.     int ocs = cs;
  300.  
  301.     if (!vfinddir) {
  302.     feep();
  303.     return;
  304.     }
  305.     if (mult < 0) {
  306.     mult = -mult;
  307.     virevrepeatfind();
  308.     return;
  309.     }
  310.     while (mult--) {
  311.     do
  312.         cs += vfinddir;
  313.     while (cs >= 0 && cs < ll && line[cs] != vfindchar && line[cs] != '\n');
  314.     if (cs < 0 || cs >= ll || line[cs] == '\n') {
  315.         feep();
  316.         cs = ocs;
  317.         return;
  318.     }
  319.     }
  320.     cs += tailadd;
  321.     if (vfinddir == 1 && virangeflag)
  322.     cs++;
  323. }
  324.  
  325. void virevrepeatfind()
  326. {                /**/
  327.     if (mult < 0) {
  328.     mult = -mult;
  329.     virepeatfind();
  330.     return;
  331.     }
  332.     vfinddir = -vfinddir;
  333.     virepeatfind();
  334.     vfinddir = -vfinddir;
  335. }
  336.  
  337. void vifirstnonblank()
  338. {                /**/
  339.     cs = findbol();
  340.     while (cs != ll && iblank(line[cs]))
  341.     cs++;
  342. }
  343.  
  344. void visetmark()
  345. {                /**/
  346.     int ch;
  347.  
  348.     ch = getkey(0);
  349.     if (ch < 'a' || ch > 'z') {
  350.     feep();
  351.     return;
  352.     }
  353.     ch -= 'a';
  354.     vimarkcs[ch] = cs;
  355.     vimarkline[ch] = histline;
  356. }
  357.  
  358. void vigotomark()
  359. {                /**/
  360.     int ch;
  361.  
  362.     ch = getkey(0);
  363.     if (ch == c)
  364.     ch = 26;
  365.     else {
  366.     if (ch < 'a' || ch > 'z') {
  367.         feep();
  368.         return;
  369.     }
  370.     ch -= 'a';
  371.     }
  372.     if (!vimarkline[ch]) {
  373.     feep();
  374.     return;
  375.     }
  376.     if (curhist != vimarkline[ch]) {
  377.     mult = vimarkline[ch];
  378.     lastcmd |= ZLE_ARG;
  379.     vifetchhistory();
  380.     if (histline != vimarkline[ch])
  381.         return;
  382.     }
  383.     cs = vimarkcs[ch];
  384. }
  385.  
  386. void vigotomarkline()
  387. {                /**/
  388.     vigotomark();
  389.     cs = findbol();
  390. }
  391.